package com.google.api.chart;

import java.util.Arrays;
import java.util.List;

import com.google.api.chart.encoding.JoinHelper;
import com.google.api.chart.encoding.SimpleURLEncoding;
import com.google.api.chart.features.ColorFeature;
import com.google.api.chart.features.FillFeature;
import com.google.api.chart.features.LegendFeature;
import com.google.api.chart.features.ScalingFeature;
import com.google.api.chart.features.TitleFeature;

public class CommonFeatureHelper<T> implements ColorFeature<T>, FillFeature<T>, ScalingFeature<T>, TitleFeature<T>, LegendFeature<T>, ChartTypeMaker {

	protected T outer;
	
	private String chtt = null;
	private String chdl = null;
	private String chco = null;
	private String chf = null;
	private String chds = null;
	private String chts = null;

	private String chma;
	
	protected CommonFeatureHelper(T outer) {
		this.outer = outer;
	}

	@Override
	public void fillParameters(List<String> parameters, List<String> chmParameters) {
		if (chtt != null)
			parameters.add("chtt=" + chtt);
		if (chts != null)
			parameters.add("chts=" + chts);
		if (chdl != null)
			parameters.add("chdl=" + chdl);
		if (chco != null)
			parameters.add("chco=" + chco);
		if (chf != null)
			parameters.add("chf=" + chf);
		if (chds != null)
			parameters.add("chds=" + chds);
		if (chma != null)
			parameters.add("chma=" + chma);
	}
	
	@Override
	public T title(String... title) {
		this.chtt  = JoinHelper.join(JoinHelper.array(JoinHelper.map(Arrays.asList(title), SimpleURLEncoding.encodeList), String.class), "|");
		return outer;
	}
	
	@Override
	public T title(ColorMaker color, int textSize, String... title) {
		title(title);
		this.chts = color.toString() + "," + textSize;
		return outer;
	}

	@Override
	public T margins(MarginMaker margins) {
		this.chma = margins.toString();
		return outer;
	}
	
	@Override
	public T legend(String... legends) {
		legend(ChartLegendMaker.legend(legends));
		return outer;
	}

	@Override
	public T legend(ChartLegendMaker legend) {
		this.chdl  = legend.toString();
		return outer;
	}
	
	@Override
	public T colors(ColorMaker... colors) {
		this.chco = JoinHelper.join(colors, ",");
		return outer;
	}
	
	@Override
	public T fills(FillMaker... fills) {
		this.chf = JoinHelper.join(fills, "|");
		return outer;
	}

	@Override
	public T scaling(ScalingMaker... scaling) {
		this.chds = JoinHelper.join(scaling, "|");
		return outer;
	}

}
